package co.salehi.web;

import java.util.Set;

import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
import org.springframework.roo.addon.web.mvc.controller.RooConversionService;

import co.salehi.domain.Battery;
import co.salehi.domain.BatteryMake;
import co.salehi.domain.Country;
import co.salehi.domain.Operator;
import co.salehi.domain.Ring;
import co.salehi.domain.Sell;
import co.salehi.domain.Tyre;
import co.salehi.domain.TyreMake;

/**
 * A central place to register application Converters and Formatters.
 */
@RooConversionService
public class ApplicationConversionServiceFactoryBean extends
		FormattingConversionServiceFactoryBean {

	@Override
	protected void installFormatters(FormatterRegistry registry) {
		super.installFormatters(registry);
		registry.addConverter(getSetConverter());
		registry.addConverter(getBatteryMakeConverter());
		registry.addConverter(getCountryConverter());
		registry.addConverter(getBatteryConverter());
        registry.addConverter(getBatteryMakeConverter());
        registry.addConverter(getCountryConverter());
        registry.addConverter(getOperatorConverter());
        registry.addConverter(getRingConverter());
        registry.addConverter(getSellConverter());
        registry.addConverter(getTyreConverter());
        registry.addConverter(getTyreMakeConverter());
	}

	org.springframework.core.convert.converter.Converter<TyreMake, String> getTyreMakeConverter() {
		return new org.springframework.core.convert.converter.Converter<TyreMake, String>() {
			public String convert(TyreMake tyremake) {
				return tyremake.getName() + " [" + tyremake.getCountry() + "]";
			}
		};
	}


	org.springframework.core.convert.converter.Converter<Set<Object>, String> getSetConverter() {
		return new org.springframework.core.convert.converter.Converter<Set<Object>, String>() {
			public String convert(Set<Object> items) {
				StringBuilder stringBuilder = new StringBuilder();
				stringBuilder.append("[");
				if (items != null)
					for (Object ring : items) {
						stringBuilder.append(ring);
						stringBuilder.append(",");
					}
				if (stringBuilder.length() > 1) {
					stringBuilder = new StringBuilder(stringBuilder.substring(
							0, stringBuilder.length() - 2));
				}
				stringBuilder.append("]");
				return stringBuilder.toString();
			}
		};
	}
	
	org.springframework.core.convert.converter.Converter<BatteryMake, String> getBatteryMakeConverter() {
        return new org.springframework.core.convert.converter.Converter<BatteryMake, String>() {
            public String convert(BatteryMake batterymake) {
                return new StringBuilder().append(batterymake.getName()).toString();
            }
        };
    }
	
	org.springframework.core.convert.converter.Converter<Country, String> getCountryConverter() {
        return new org.springframework.core.convert.converter.Converter<Country, String>() {
            public String convert(Country country) {
                return new StringBuilder().append(country.getName()).toString();
            }
        };
    }
	
    Converter<Battery, String> getBatteryConverter() {
        return new Converter<Battery, String>() {
            public String convert(Battery battery) {
                return new StringBuilder().append(battery.getAmper()).append(" ").append(battery.getBuyPrice()).append(" ").append(battery.getSellPrice()).append(" ").append(battery.getReceiptPrice()).toString();
            }
        };
    }
    
    org.springframework.core.convert.converter.Converter<Operator, String> getOperatorConverter() {
        return new org.springframework.core.convert.converter.Converter<Operator, String>() {
            public String convert(Operator operator) {
                return new StringBuilder().append(operator.getFirstName()).append(" ").append(operator.getLastName()).append(" ").append(operator.getUserName()).append(" ").append(operator.getPassword()).toString();
            }
        };
    }
    
    org.springframework.core.convert.converter.Converter<Ring, String> getRingConverter() {
        return new org.springframework.core.convert.converter.Converter<Ring, String>() {
            public String convert(Ring ring) {
                return new StringBuilder().append(ring.getName()).append(" ").append(ring.getBuyPrice()).append(" ").append(ring.getSellPrice()).append(" ").append(ring.getReceiptPrice()).toString();
            }
        };
    }
    
    org.springframework.core.convert.converter.Converter<Sell, String> getSellConverter() {
        return new org.springframework.core.convert.converter.Converter<Sell, String>() {
            public String convert(Sell sell) {
                return new StringBuilder().append(sell.getName()).append(" ").append(sell.getSellDate()).toString();
            }
        };
    }
    
    org.springframework.core.convert.converter.Converter<Tyre, String> getTyreConverter() {
        return new org.springframework.core.convert.converter.Converter<Tyre, String>() {
            public String convert(Tyre tyre) {
                return new StringBuilder().append(" ").append(tyre.getDesign()).append(" ").append(tyre.getBuyPrice()).append(" ").append(tyre.getSellPrice()).toString();
            }
        };
    }


}
